<?php
namespace Home\Controller;
use Think\Controller;
class QuestionController extends Controller {
  public function add(){
    $sql = "select * from vercode order by rand() limit 1";//任意排序取第一条
    $res = M()->query($sql);  //实例化操作数据库的类用其中的query函数
    $_SESSION['answer']=$res[0]['answer'];  //数据库的答案放在session中

    $this->assign('res',$res);      
    //将数组$res放在全局变量res那里，这样html就可以使用$res了

    $res2 = M('category')->select();
    $this->assign('category',$res2);
    $this->display();
  }

   public function upload()
   {
      $upload = new \Think\Upload();// 实例化上传类
      $upload->maxSize   =     3145728 ;// 设置附件上传大小
      $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
      $upload->rootPath  =     './Uploads/question_img/'; 
      // 设置附件上传根目录
      $upload->savePath  =     ''; // 设置附件上传（子）目录
      // 上传文件 
      $info   =   $upload->upload();
      if(!$info) {// 上传错误提示错误信息
          $this->error($upload->getError());
      }else{// 上传成功
        $arr = array(
            'code'=>0,
            'msg'=>'上传成功',
            'data'=>array(
              'src'=>__ROOT__."/Uploads/question_img/".$info['file']['savepath'].$info['file']['savename'],
              'title'=>'sdfsdf'
              ),
          );
        echo json_encode($arr);
  
      }

   }
   
   // 发布
   public function publish(){
    
      $neiyong =I('post.');
      $a=$_SESSION['answer'];

      if ($neiyong['vercode'] != $a) {
        $arr=array(
          'error'=>1,
          'info'=>"答案错误"
          );
        echo json_encode($arr);
      }else{
        $neiyong['create_time']=time();
        $neiyong['uid']=$_SESSION['uid'];
        $neiyong['cid']=$neiyong['class'];
        $res = M('question')->add($neiyong);

        if ($res) {
          $arr=array(
          'error'=>0,
          'info'=>"发布成功"
          );
        echo json_encode($arr);
        }else{
          $arr=array(
          'error'=>1,
          'info'=>"发布失败"
          );
          echo json_encode($arr);
        }


      }
      
  
      

   }
   public function show(){
    $qid=I('get.qid','');
    $sql="select t1.nikename,t1.face,t1.id,t2.* from user t1 inner join question t2 on t1.id=t2.uid where t2.id='{$qid}'";
    $res=current(M()->query($sql));

    $this->assign('res',$res);

    $answer_sql="select t3.face,t3.nikename,t2.create_time,t2.content,t2.zan,t2.uid from question t1 inner join answer t2 on t1.id=t2.qid inner join user t3 on t3.id=t1.uid where t2.uid='{$id}'";
    $answer_res=M()->query($answer_sql);
    $this->assign('answer_res',$answer_res);

    $this->display();
   }

   public function addanswer(){
      $neirong=I('post.','');

      if (empty($neirong['content']) ) {
        $arr=array(
          'error'=>1,
          'info'=>"不能为空"
          );
          echo json_encode($arr);
      }else{
        $neirong['uid']=$_SESSION['uid'];
        $neirong['create_time']=time();
        $res = M('answer')->add($neirong);
        if ($res) {
          $qid=I('get.qid','');
          


          $arr=array(
          'error'=>0,
          'info'=>"上传成功"
          );
          echo json_encode($arr);
        }else{
          $arr=array(
          'error'=>1,
          'info'=>"上传失败"
          );
          echo json_encode($arr);
        }

      }

   }

   








}
